package poker.model;

import poker.PokerUtil;

import at.ac.tuwien.big.easyholdem.game.Action;
import at.ac.tuwien.big.easyholdem.game.Game;
import at.ac.tuwien.big.easyholdem.game.State;
import at.ac.tuwien.big.easyholdem.player.DerbyPlayerDAO;
import at.ac.tuwien.big.easyholdem.player.Player;
import at.ac.tuwien.big.easyholdem.poker.Card;


public class GameBean
{
	private Game game;
	private Player player;
	
	private Boolean foldActive;
	private Boolean checkActive;
	private Boolean betActive;
	private String pathPlayer0;
	private String pathPlayer1;
	private String pathRiver;
	private String pathTurn;
	private String pathFlop0;
	private String pathFlop1;
	private String pathFlop2;
	private String pathDealer0;
	private String pathDealer1;
	private String altDealer0;
	private String altDealer1;
	private String altFlop0;
	private String altFlop1;
	private String altFlop2;
	private String altTurn;
	private String altRiver;
	private String altPlayer0;
	private String altPlayer1;
	private String infoText;
	private Integer potSize;
	private Integer fixedBetSize;
	private Integer playerStack;
	private DerbyPlayerDAO derbyPlayerDAO;
	
	public DerbyPlayerDAO getDerbyPlayerDAO() {
		return derbyPlayerDAO;
	}

	public void setDerbyPlayerDAO(DerbyPlayerDAO derbyPlayerDAO) {
		this.derbyPlayerDAO = derbyPlayerDAO;
	}

	public Player getPlayer() {
		return player;
	}

	public void setPlayer(Player player) {
		this.player = player;
	}

	public void newTable()
	{
		game = new Game();
		derbyPlayerDAO.update(player); 
		foldActive = checkActive = betActive = false;		
		pathPlayer0 = pathPlayer1 = pathRiver = pathTurn = "img/deck/bg.png";
		pathFlop0 = pathFlop1 = pathFlop2 = pathDealer0 = pathDealer1 = "img/deck/bg.png";
		altDealer0 = altDealer1 = altFlop0 = altFlop1 = altFlop2 = getAlt(pathPlayer0);
		altTurn = altRiver = altPlayer0 = altPlayer1 = getAlt(pathPlayer0);
		infoText = "+++ Neues Turnier am 23. Juni 1983 +++";
		fixedBetSize = PokerUtil.DEFAULT_BET_SIZE;
		playerStack = player.getStack();
		potSize = 0;
	}
	
	public void newGame()
	{
		game = new Game(player,PokerUtil.DEFAULT_BET_SIZE);
		
		if (game.getPlayer().getStack() < PokerUtil.DEFAULT_BET_SIZE * 10)
		{
			game.getPlayer().setStack(PokerUtil.DEFAULT_STACK_SIZE);
		}
		
		game.restart();
		game.act(Action.ANTE);
		
		init();
		
	}
	
	public void bet()
	{
		if(game.getPossibleActions().contains(Action.BET))
			game.act(Action.BET);
		
		init();
	}
	
	public void check()
	{
		if(game.getPossibleActions().contains(Action.CHECK))
			game.act(Action.CHECK);
		
		init();
	}
	
	public void fold()
	{
		if(game.getPossibleActions().contains(Action.FOLD))
			game.act(Action.FOLD);
		
		init();
	}
	
	private void init()
	{
		  pathPlayer0 = getPathPlayer(0);
		  pathPlayer1 = getPathPlayer(1);
		  foldActive = isFoldActive();
		  checkActive = isCheckActive(); 
		  betActive = isBetActive();
		  pathRiver = getPathRiver();
		  pathTurn = getPathRiver();
		  pathFlop0 = getPathFlop(0);
		  pathFlop1 = getPathFlop(1);
		  pathFlop2 = getPathFlop(2);
		  pathDealer0 = getPathDealer(0);
		  pathDealer1 = getPathDealer(1);
		  altPlayer0 = getAlt(pathPlayer0);
		  altPlayer1 = getAlt(pathPlayer1);
		  altDealer0 = getAlt(pathDealer0);
		  altDealer1 = getAlt(pathDealer1);
		  altFlop0 = getAlt(pathFlop0);
		  altFlop1 = getAlt(pathFlop1);
		  altFlop2 = getAlt(pathFlop2);
		  altTurn = getAlt(pathTurn);
		  altRiver = getAlt(pathRiver);
		  infoText = getInfo();  
		  potSize = game.getPotSize();
		  fixedBetSize = game.getFixedBetSize();
		  playerStack = game.getPlayer().getStack();
	}
	
	public boolean isBetActive()
	{
		return game.getState().equals(State.PRE_FLOP) || game.getState().equals(State.PRE_TURN) || game.getState().equals(State.PRE_RIVER) ? true : false;
	}

	public boolean isCheckActive()
	{
		return game.getState().equals(State.PRE_TURN) || game.getState().equals(State.PRE_RIVER) ? true : false;
	}

	public boolean isFoldActive()
	{
		return game.getState().equals(State.PRE_FLOP) ? true : false;
	}

	public String getPathFlop(int pos)
	{
		return "img/deck/" + (!game.getFlopCards().isEmpty() ? getPath(game.getFlopCards().get(pos)) : "bg") + ".png";
	}

	public String getPathTurn()
	{
		return "img/deck/" + ((game.getTurnCard() != null) ? getPath(game.getTurnCard()) : "bg") + ".png";
	}

	public String getPathRiver()
	{
		return "img/deck/" + ((game.getRiverCard() != null) ? getPath(game.getRiverCard()) : "bg") + ".png";
	}

	public String getPathDealer(int pos)
	{
		return "img/deck/" + (game.getState() == State.END && !game.getFlopCards().isEmpty() ? getPath(game.getShowdownResult().getDealersCards().get(pos)) : "bg") + ".png";
	}

	public String getPathPlayer(int pos)
	{
		return "img/deck/" + (!game.getPlayersCards().isEmpty() ? getPath(game.getPlayersCards().get(pos)) : "bg") + ".png";
	}
	
	private String getPath(Card card)
	{
		return card.getSuit().name().toLowerCase() + "-" + PokerUtil.getConvertedRank(card.getRank());
	}

	public String getAlt(String path)
	{
		return !path.endsWith("bg.png") ? path.substring("img/deck".length() + 1, path.length() - 4) : "unknown";
	}


	public String getInfo()
	{
		String infoText = "+++ Neues Turnier am 23. Juni 1983 +++";

		if (game.getState() == State.END) {
			derbyPlayerDAO.update(player);
			
			if (game.getShowdownResult().hasPlayerWon())
				infoText = "Sie haben mit " + game.getShowdownResult().getPlayersHandRank().getRank() + " gegen " + game.getShowdownResult().getDealersHandRank().getRank() + " GEWONNEN.";
			else
				if (game.getShowdownResult().isSplitPot())
					infoText = "Der Pot wird geteilt (" + game.getShowdownResult().getDealersHandRank().getRank() + ").";
				else
					if (game.getShowdownResult().getPlayersHandRank() == null)
						infoText = "Sie haben aufgegeben und ihren Einsatz verloren.";
					else
						infoText = "Sie haben mit " + game.getShowdownResult().getPlayersHandRank().getRank() + " gegen " + game.getShowdownResult().getDealersHandRank().getRank() + " VERLOREN.";
		}

		return infoText;
	}

	public Boolean getFoldActive() {
		return foldActive;
	}

	public void setFoldActive(Boolean foldActive) {
		this.foldActive = foldActive;
	}

	public Boolean getCheckActive() {
		return checkActive;
	}

	public void setCheckActive(Boolean checkActive) {
		this.checkActive = checkActive;
	}

	public Boolean getBetActive() {
		return betActive;
	}

	public void setBetActive(Boolean betActive) {
		this.betActive = betActive;
	}

	public String getPathPlayer0() {
		return pathPlayer0;
	}

	public void setPathPlayer0(String pathPlayer0) {
		this.pathPlayer0 = pathPlayer0;
	}

	public String getPathPlayer1() {
		return pathPlayer1;
	}

	public void setPathPlayer1(String pathPlayer1) {
		this.pathPlayer1 = pathPlayer1;
	}

	public String getPathFlop0() {
		return pathFlop0;
	}

	public void setPathFlop0(String pathFlop0) {
		this.pathFlop0 = pathFlop0;
	}

	public String getPathFlop1() {
		return pathFlop1;
	}

	public void setPathFlop1(String pathFlop1) {
		this.pathFlop1 = pathFlop1;
	}

	public String getPathFlop2() {
		return pathFlop2;
	}

	public void setPathFlop2(String pathFlop2) {
		this.pathFlop2 = pathFlop2;
	}

	public String getPathDealer0() {
		return pathDealer0;
	}

	public void setPathDealer0(String pathDealer0) {
		this.pathDealer0 = pathDealer0;
	}

	public String getPathDealer1() {
		return pathDealer1;
	}

	public void setPathDealer1(String pathDealer1) {
		this.pathDealer1 = pathDealer1;
	}

	public String getAltDealer0() {
		return altDealer0;
	}

	public void setAltDealer0(String altDealer0) {
		this.altDealer0 = altDealer0;
	}

	public String getAltDealer1() {
		return altDealer1;
	}

	public void setAltDealer1(String altDealer1) {
		this.altDealer1 = altDealer1;
	}

	public String getAltFlop0() {
		return altFlop0;
	}

	public void setAltFlop0(String altFlop0) {
		this.altFlop0 = altFlop0;
	}

	public String getAltFlop1() {
		return altFlop1;
	}

	public void setAltFlop1(String altFlop1) {
		this.altFlop1 = altFlop1;
	}

	public String getAltFlop2() {
		return altFlop2;
	}

	public void setAltFlop2(String altFlop2) {
		this.altFlop2 = altFlop2;
	}

	public String getAltTurn() {
		return altTurn;
	}

	public void setAltTurn(String altTurn) {
		this.altTurn = altTurn;
	}

	public String getAltRiver() {
		return altRiver;
	}

	public void setAltRiver(String altRiver) {
		this.altRiver = altRiver;
	}

	public String getAltPlayer0() {
		return altPlayer0;
	}

	public void setAltPlayer0(String altPlayer0) {
		this.altPlayer0 = altPlayer0;
	}

	public String getAltPlayer1() {
		return altPlayer1;
	}

	public void setAltPlayer1(String altPlayer1) {
		this.altPlayer1 = altPlayer1;
	}

	public String getInfoText() {
		return infoText;
	}

	public void setInfoText(String infoText) {
		this.infoText = infoText;
	}

	public Integer getPotSize() {
		return potSize;
	}

	public void setPotSize(Integer potSize) {
		this.potSize = potSize;
	}

	public Integer getFixedBetSize() {
		return fixedBetSize;
	}

	public void setFixedBetSize(Integer fixedBetSize) {
		this.fixedBetSize = fixedBetSize;
	}

	public Integer getPlayerStack() {
		return playerStack;
	}

	public void setPlayerStack(Integer playerStack) {
		this.playerStack = playerStack;
	}

	public void setPathRiver(String pathRiver) {
		this.pathRiver = pathRiver;
	}

	public void setPathTurn(String pathTurn) {
		this.pathTurn = pathTurn;
	}
	
	
}
